package com.ruoyi.information.service.back.impl;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.information.domain.back.ServiceArticleBack;
import com.ruoyi.information.domain.pc.SysUser;
import com.ruoyi.information.mapper.back.RecomBackMapper;
import com.ruoyi.information.mapper.back.ServiceArticleBackMapper;
import com.ruoyi.information.mapper.pc.ServiceCenterMapper;
import com.ruoyi.information.mapper.pc.SysUserServiceMapper;
import com.ruoyi.information.service.back.IServiceArticleBackService;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.PathVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务资讯Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-05
 */
@Service
public class ServiceArticleBackServiceImpl implements IServiceArticleBackService {
    @Autowired
    private ServiceArticleBackMapper serviceArticleMapper;

    @Autowired
    private RemoteFileService remoteFileService;

    @Autowired
    private RecomBackMapper recomBackMapper;

    @Autowired
    private SysUserServiceMapper serviceMapper;

    @Autowired
    private ServiceCenterMapper serviceCenterMapper;


    /**
     * 查询服务资讯
     *
     * @param id 服务资讯主键
     * @return 服务资讯
     */
    @Override
    public ServiceArticleBack selectServiceArticleById(Long id) {
        ServiceArticleBack serviceArticleBack = serviceArticleMapper.selectServiceArticleById(id);
        if (serviceArticleBack != null) {
            SysUser sysUser = new SysUser();
            sysUser.setDeptId(id);
            List<SysUser> sysUsers = serviceMapper.selectSysUserList(sysUser);
            //查询联系人
            if (sysUsers != null && !sysUsers.isEmpty()) {
                serviceArticleBack.setUserList(sysUsers);
            }

            try {
                String[] split = serviceArticleBack.getServiceId().split(",");
                List<Long> ids = new ArrayList<>();
                for (String s : split) {
                    ids.add(Long.valueOf(s));
                }
                serviceArticleBack.setIds(ids);
            } catch (Exception ignored) {
            }

        }

        return serviceArticleBack;
    }

    /**
     * 查询服务资讯列表
     *
     * @param serviceArticle 服务资讯
     * @return 服务资讯
     */
    @Override
    public List<ServiceArticleBack> selectServiceArticleList(ServiceArticleBack serviceArticle) {

        //新多选
        if (serviceArticle.getIds() != null && !serviceArticle.getIds().isEmpty()) {
            List<Long> ids = new ArrayList<>();

            for (Long id : serviceArticle.getIds()) {
                //父查子
                List<Long> idss = serviceCenterMapper.selectServiceCenterIdsById(id);
                if (idss != null && !idss.isEmpty()) {
                    ids.add(id);
                    ids.addAll(idss);
                }
                //子查父
                List<Long> idsp = serviceCenterMapper.selectServiceCenterParentIdsById(id);
                if (idsp != null && !idsp.isEmpty()) {
                    ids.addAll(idsp);
                }
            }

            serviceArticle.setIds(ids);

        } else {
            //旧单选
            if (serviceArticle.getServiceId() != null) {
                //父查子
                List<Long> ids = serviceCenterMapper.selectServiceCenterIdsById(Long.valueOf(serviceArticle.getServiceId()));
                ids.add(Long.valueOf(serviceArticle.getServiceId()));
                serviceArticle.setIds(ids);
                //子查父
                List<Long> idsp = serviceCenterMapper.selectServiceCenterParentIdsById(Long.valueOf(serviceArticle.getServiceId()));
                if (idsp != null && !idsp.isEmpty()) {
                    ids.addAll(idsp);
                }
            }
        }

        return serviceArticleMapper.selectServiceArticleList(serviceArticle);
    }

    /**
     * 新增服务资讯
     *
     * @param serviceArticle 服务资讯
     * @return 结果
     */
    @Override
    public int insertServiceArticle(ServiceArticleBack serviceArticle) {
        serviceArticle.setCreateTime(DateUtils.getNowDate());


        try {
            if (serviceArticle.getIds() != null && !serviceArticle.getIds().isEmpty()) {
                serviceArticle.setServiceId(StringUtils.join(serviceArticle.getIds(), ","));
            }
        } catch (Exception ignored) {

        }

        int i = serviceArticleMapper.insertServiceArticle(serviceArticle);

        try {
            if (serviceArticle.getUserList() != null && !serviceArticle.getUserList().isEmpty()) {
                List<SysUser> userList = serviceArticle.getUserList();
                for (SysUser sysUser : userList) {
                    sysUser.setDeptId(serviceArticle.getId());
                    serviceMapper.insertSysUser(sysUser);
                }
            }
        } catch (Exception ignored) {

        }

        return i;
    }

    /**
     * 修改服务资讯
     *
     * @param serviceArticle 服务资讯
     * @return 结果
     */
    @Override
    public int updateServiceArticle(ServiceArticleBack serviceArticle) {
        serviceArticle.setUpdateTime(DateUtils.getNowDate());

        /*删除云资源*/
        if (serviceArticle.getCover() != null) {
            ServiceArticleBack main = serviceArticleMapper.selectServiceArticleById(serviceArticle.getId());
            if (main.getCover() != null) {
                CloudClassroomBackServiceImpl.comparePhoto(main.getCover(), serviceArticle.getCover(), remoteFileService);
            }
        }

        try {
            if (serviceArticle.getIds() != null && !serviceArticle.getIds().isEmpty()) {
                serviceArticle.setServiceId(StringUtils.join(serviceArticle.getIds(), ","));
            }
        } catch (Exception ignored) {

        }

        try {
            if (serviceArticle.getUserList() != null && !serviceArticle.getUserList().isEmpty()) {
                serviceMapper.deleteSysUserByUserId(serviceArticle.getId());
                List<SysUser> userList = serviceArticle.getUserList();
                for (SysUser sysUser : userList) {
                    sysUser.setDeptId(serviceArticle.getId());
                    serviceMapper.insertSysUser(sysUser);
                }
            }
        } catch (Exception ignored) {
        }


        return serviceArticleMapper.updateServiceArticle(serviceArticle);
    }

    /**
     * 批量删除服务资讯
     *
     * @param ids 需要删除的服务资讯主键
     * @return 结果
     */
    @Override
    public int deleteServiceArticleByIds(Long[] ids) {
        /*删除云资源*/
        List<String> photos = serviceArticleMapper.selectServiceArticleListPhotos(ids);
        if (photos != null) {
            remoteFileService.delete(new PathVo(photos), SecurityConstants.FROM_SOURCE);
        }
        recomBackMapper.deleteRecomByIdsAndType(ids, "服务资讯");

        try {
            //删除联系人
            serviceMapper.deleteSysUserByUserIds(ids);
        } catch (Exception ignored) {
        }


        return serviceArticleMapper.deleteServiceArticleByIds(ids);
    }

    /**
     * 删除服务资讯信息
     *
     * @param id 服务资讯主键
     * @return 结果
     */
    @Override
    public int deleteServiceArticleById(Long id) {
        return serviceArticleMapper.deleteServiceArticleById(id);
    }
}
